/****************************************************************************
 *
 *   Copyright (c) 2019-2021 PX4 Development Team. All rights reserved.
 *   Copyright (c) 2025 H743 Flight Controller Project. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 * 3. Neither the name PX4 nor the names of its contributors may be
 *    used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ****************************************************************************/

#pragma once

#include <stdint.h>

namespace px4
{

class WorkQueue; // forward declaration

/**
 * Work queue configuration structure
 */
struct wq_config_t {
	const char *name;           // Work queue name
	uint16_t stacksize;         // Stack size in bytes
	int8_t relative_priority;   // Priority relative to max (negative = lower priority)
};

/**
 * Predefined work queue configurations
 */
namespace wq_configurations
{

// Rate control work queue (highest priority for control loops)
static constexpr wq_config_t rate_ctrl{"wq:rate_ctrl", 2048, 0};

// SPI bus work queues (one per SPI bus for sensor isolation)
static constexpr wq_config_t SPI0{"wq:SPI0", 1536, -1};
static constexpr wq_config_t SPI1{"wq:SPI1", 1536, -1};
static constexpr wq_config_t SPI2{"wq:SPI2", 1536, -1};
static constexpr wq_config_t SPI3{"wq:SPI3", 1536, -1};
static constexpr wq_config_t SPI4{"wq:SPI4", 1536, -1};
static constexpr wq_config_t SPI5{"wq:SPI5", 1536, -1};
static constexpr wq_config_t SPI6{"wq:SPI6", 1536, -1};

// I2C bus work queues
static constexpr wq_config_t I2C0{"wq:I2C0", 1536, -2};
static constexpr wq_config_t I2C1{"wq:I2C1", 1536, -2};
static constexpr wq_config_t I2C2{"wq:I2C2", 1536, -2};
static constexpr wq_config_t I2C3{"wq:I2C3", 1536, -2};
static constexpr wq_config_t I2C4{"wq:I2C4", 1536, -2};

// Navigation and controllers (high priority)
static constexpr wq_config_t nav_and_controllers{"wq:nav_and_controllers", 2048, -1};

// INS (Inertial Navigation System) work queues
static constexpr wq_config_t INS0{"wq:INS0", 2048, -1};
static constexpr wq_config_t INS1{"wq:INS1", 2048, -1};
static constexpr wq_config_t INS2{"wq:INS2", 2048, -1};
static constexpr wq_config_t INS3{"wq:INS3", 2048, -1};

// High priority default
static constexpr wq_config_t hp_default{"wq:hp_default", 1792, -2};

// UAVCAN work queue
static constexpr wq_config_t uavcan{"wq:uavcan", 1536, -3};

// Serial port work queues
static constexpr wq_config_t ttyS0{"wq:ttyS0", 1536, -10};
static constexpr wq_config_t ttyS1{"wq:ttyS1", 1536, -10};
static constexpr wq_config_t ttyS2{"wq:ttyS2", 1536, -10};
static constexpr wq_config_t ttyS3{"wq:ttyS3", 1536, -10};
static constexpr wq_config_t ttyS4{"wq:ttyS4", 1536, -10};
static constexpr wq_config_t ttyS5{"wq:ttyS5", 1536, -10};
static constexpr wq_config_t ttyS6{"wq:ttyS6", 1536, -10};
static constexpr wq_config_t ttyACM0{"wq:ttyACM0", 1536, -10};
static constexpr wq_config_t ttyUnknown{"wq:ttyUnknown", 1536, -10};

// Low priority default
static constexpr wq_config_t lp_default{"wq:lp_default", 1536, -50};

// Test work queues
static constexpr wq_config_t test1{"wq:test1", 2000, 0};
static constexpr wq_config_t test2{"wq:test2", 2000, 0};

} // namespace wq_configurations

/**
 * Start the work queue manager task.
 * 
 * @return 0 on success, negative errno on failure
 */
int WorkQueueManagerStart();

/**
 * Stop the work queue manager task.
 * 
 * @return 0 on success, negative errno on failure
 */
int WorkQueueManagerStop();

/**
 * Print work queue manager status.
 * 
 * @return 0 on success, negative errno on failure
 */
int WorkQueueManagerStatus();

/**
 * Create (or find) a work queue with a particular configuration.
 *
 * @param new_wq  The work queue configuration
 * @return        A pointer to the WorkQueue, or nullptr on failure
 */
WorkQueue *WorkQueueFindOrCreate(const wq_config_t &new_wq);

/**
 * Map a device bus to a work queue configuration.
 *
 * @param bus_type  Bus type (SPI, I2C, etc.)
 * @param bus       Bus number
 * @return          A work queue configuration
 */
const wq_config_t &device_bus_to_wq(uint8_t bus_type, uint8_t bus);

} // namespace px4

